package com.hf.test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.struts2.StrutsSpringJUnit4TestCase;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hf.base.shh.BaseActionGen;
import com.hf.base.shh.BaseForm;
import com.hf.base.shh.BaseService;
import com.opensymphony.xwork2.interceptor.annotations.Before;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml", "classpath:hibernate.xml" })
public abstract class BaseTestAction <EAction extends 
BaseActionGen<EFrom,Eservice, Eentiy>

,EFrom extends BaseForm<Eentiy>,Eservice extends BaseService<EFrom,Eentiy>,
Eentiy> 

extends StrutsSpringJUnit4TestCase<EAction>{

	EAction action;
	EFrom  form;
	Eentiy entity;
	Class<EAction> actionClass;
	Class<EFrom> formClass;
	Class<Eentiy> entityClass;
	@Before
	public void init()
	{

		Type type=  getClass().getGenericSuperclass();
		Type[] params =((ParameterizedType)type).getActualTypeArguments(); 
		actionClass =(Class<EAction>) params[0];
		formClass=(Class<EFrom>) params[1]; 
		entityClass =(Class<Eentiy>) params[3]; 
		
		try {
			 
			form = formClass.newInstance();
			entity =entityClass.newInstance();			
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException("反射出错了！");
		}
		try {
			initAction();

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
		action = getAction();
		form.setEntity(entity);
		action.setPageEntity(form);
	}
	public void printEntities(){
		String string="";
		List<Eentiy> list = form.getEntities();
		for (Eentiy eentiy : list) {
			Field[] fields=entityClass.getDeclaredFields();
			Method[] methods = entityClass.getDeclaredMethods();
			string ="";
			for (Method method : methods) {
				//System.out.println(method.getName());
				try {
					if (method.getName().startsWith("get")) {
						string =string+ method.getName()+" :"+(method.invoke(eentiy)==null?"":method.invoke(eentiy, null).toString());
					System.out.println("string: "+string);
						//string =string+ method.getName();
					}
					
			 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(	string);
			System.out.println(	"***********************************************");
			
		}
		System.out.println(	"***********************************************");
		Eentiy es = form.getEntity();
		if (!(es==null)) {
			Field[] fields=entityClass.getDeclaredFields();
			Method[] methods = entityClass.getDeclaredMethods();
			string="";
			for (Method method : methods) {
				//System.out.println(method.getName());
				try {
					if (method.getName().startsWith("get")&&es!=null) {
						string =string+ method.getName()+" :"+	(method.invoke(es, null)==null?"":method.invoke(es, null).toString());
					
					}
					
							 
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			System.out.println(	string);
			
		}

				
		
	}
	public abstract void initAction()throws Exception, ServletException;
	 

}
